package com.suxin.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author Tang
 * @classname MyRealm
 * @description [  ]
 * @date 2021/3/20 13:28
 */
public class MyRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("身份验证...");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String principal = token.getPrincipal().toString();
        String credential = token.getCredentials().toString();
        // 查询数据库
        if ("name".equals(principal)) {
            return new SimpleAuthenticationInfo(principal, "tang", this.getName());
        }
        return new SimpleAuthenticationInfo();
    }
}